package vip.xiaonuo.issued.impl.device;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.facedoor.iservice.IDeviceBaseService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.param.DeviceBaseParam;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.deviceset.entity.DwDeviceSet;
import vip.xiaonuo.facedoor.modular.deviceset.service.DwDeviceSetService;
import vip.xiaonuo.facedoor.modular.memberdevice.entity.MemberDevice;
import vip.xiaonuo.facedoor.modular.memberdevice.service.MemberDeviceService;
import vip.xiaonuo.facedoor.modular.orgdevice.entity.OrgDevice;
import vip.xiaonuo.facedoor.modular.orgdevice.service.OrgDeviceService;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.service.VisitorDeviceService;
import vip.xiaonuo.facedoor.remote.iservice.adapter.MemberReqAdapter;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import vip.xiaonuo.facedoor.vo.dewo.GetPersonResponse.DataDTO.PersonnelListDTO;
import vip.xiaonuo.sys.modular.timer.param.SysTimersParam;
import vip.xiaonuo.sys.modular.timer.service.SysTimersService;

@Service(value = RuleConstant.DW + RuleConstant.DEVICE_BASE)
public class MqttDeviceBaseService implements IDeviceBaseService {


  @Autowired
  private MemberReqAdapter personService;

  @Autowired
  private OrgDeviceService orgDeviceService;

  @Autowired
  private MemberDeviceService memberDeviceService;

  @Autowired
  private SysTimersService sysTimersService;

  @Autowired
  private VisitorDeviceService visitorDeviceService;

  @Autowired
  private DeviceBaseService deviceBaseService;



    @Autowired
    private DwDeviceSetService dwDeviceSetService;

    @Override
    public PageResult<PersonnelListDTO> listPerson(Long deviceId,String name) {
        throw new ServiceException(500, "暂时不支持此操作!");
    }

    @Override
    public void delete(DeviceBaseParam deviceBaseParam) {
        DeviceBase deviceBase = deviceBaseService.getById(deviceBaseParam.getId());
        // 强制删除 不清空设备人员
        if ((deviceBase.getCabinetIp() == null || deviceBase.getCabinetPort() == null)
                || (deviceBaseParam.getMandatory() != null && ObjectUtil.equal(deviceBaseParam.getMandatory(), 1))) {
            deviceBaseService.removeById(deviceBaseParam.getId());
            orgDeviceService.remove(new LambdaQueryWrapper<OrgDevice>().eq(OrgDevice::getDeviceId, deviceBase.getId()));
            memberDeviceService.remove(
                    new LambdaQueryWrapper<MemberDevice>().eq(MemberDevice::getDeviceId, deviceBase.getId()));
            visitorDeviceService.remove(
                    new LambdaQueryWrapper<VisitorDevice>().eq(VisitorDevice::getDeviceId, deviceBase.getId()));
            dwDeviceSetService.remove(new LambdaQueryWrapper<DwDeviceSet>().eq(DwDeviceSet::getDeviceBaseId,deviceBase.getId()));
            // 定时任务删除
            SysTimersParam sysTimersParam = new SysTimersParam();
            sysTimersParam.setId(deviceBase.getTaskId());
            sysTimersService.delete(sysTimersParam);
            return;
        }
        try {
            deviceBaseService.removeById(deviceBaseParam.getId());
            orgDeviceService.remove(new LambdaQueryWrapper<OrgDevice>().eq(OrgDevice::getDeviceId, deviceBase.getId()));
            memberDeviceService.remove(
                new LambdaQueryWrapper<MemberDevice>().eq(MemberDevice::getDeviceId, deviceBase.getId()));
            visitorDeviceService.remove(
                new LambdaQueryWrapper<VisitorDevice>().eq(VisitorDevice::getDeviceId, deviceBase.getId()));
            dwDeviceSetService.remove(new LambdaQueryWrapper<DwDeviceSet>().eq(DwDeviceSet::getDeviceBaseId,deviceBase.getId()));
            // 定时任务删除
            SysTimersParam sysTimersParam = new SysTimersParam();
            sysTimersParam.setId(deviceBase.getTaskId());
            sysTimersService.delete(sysTimersParam);
            personService.deleteAllPerson(deviceBaseParam.getId() + "");
        } catch (Exception e) {
            throw new ServiceException(500, "删除设备失败 失败信息：" + ExceptionUtil.getMessage(e));
        }
    }

    @Override
    public void setUserPwd(DeviceBaseParam deviceBaseParam) {

    }

    @Override
    public void setFactoryDefault(DeviceBaseParam deviceBaseParam) {

    }

    @Override
    public void setSysTime(DeviceBaseParam deviceBaseParam) {

    }

    @Override
    public void openDoor(DeviceBaseParam deviceBaseParam) {

    }
}
